/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.businessLayer;

import fri.entiry.FrPaymentcode;

import fri.entiry.FrCurrency;
import fri.entiry.FrHistory;
import fri.entiry.FrPaymentcode;
import fri.entiry.FrUser;
import fri.entiry.FrUsermoney;
import java.util.Date;
import java.util.UUID;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author NguyenDongUIT
 */
@Stateless
public class PaymentCodeBean extends GenericDAO<FrPaymentcode> implements PaymentCodeBeanRemote {

    @Override
    public int save(FrPaymentcode entity) {
        return super.save(entity);
    }

    @Override
    public int delete(FrPaymentcode entity) {
        return super.delete(entity);
    }

    @Override
    public int update(FrPaymentcode entity) {
        return super.update(entity);
    }

    @Override
    public FrPaymentcode find(int entityID) {
        return super.find(entityID);
    }

    @Override
    public Vector<FrPaymentcode> listToVector(List<FrPaymentcode> list) {
        return super.listToVector(list);
    }

    
    public PaymentCodeBean() {
        super(FrPaymentcode.class);
    }

    public PaymentCodeBean(Class<FrPaymentcode> entityClass) {
        super(entityClass);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public String generateCode(String fromUserId, String toUserId, int currencyId, float val, String description, int status) {
        
        
            
            FrPaymentcode paymentCode = new FrPaymentcode();

            UUID u = UUID.randomUUID();
            paymentCode.setCodeId(u.toString());

            FrCurrency curr = new FrCurrency();
            curr.setCurrencyId(currencyId);
            paymentCode.setCurrencyId(curr);


            paymentCode.setDateCreated(new Date());

            FrUser toUser = new FrUser();
            toUser.setUserId(toUserId);
            paymentCode.setToUserId(toUser);

            FrUser fromUser = new FrUser();
            fromUser.setUserId(fromUserId);
            paymentCode.setFromUserId(fromUser);

            UUID a = UUID.randomUUID();
            String paymentCodeStr = a.toString().substring(0, 6);
            paymentCode.setPaymentCode(paymentCodeStr);


            paymentCode.setDescription(description);

            paymentCode.setVal(val);

            paymentCode.setStatus(status);

            this.save(paymentCode);
                      

            return paymentCodeStr;
       
    }
    

    @Override
    public List<FrPaymentcode> getPaymentCodeByUserId(String userId) {
        try {
            

            Query q = em.createQuery("select s from FrPaymentcode s where s.fromUserId.userId like '" + userId + "'");
            
            List<FrPaymentcode> result = (List<FrPaymentcode>)q.getResultList();
            
            return result;
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public FrPaymentcode getPaymentcode(String codeId) {
        try {
            
            
            Query q = em.createQuery("select s from FrPaymentcode s where s.codeId like '"+codeId+"'");
            
            FrPaymentcode result = (FrPaymentcode)q.getResultList().get(0);
            
            return result;
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    
    
    @Override
    public int setStatusOfPayment(String codeId, int state) {
        try {
           
            return em.createQuery("UPDATE FrPaymentcode c SET  c.status = '" + state + "' where c.codeId like '" + codeId + "'").executeUpdate();
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return 0;
        }
    }

    @Override
    public int applyPaymentCode(String toUserId, String paymentCode) {
        try {
            
            
            
            FrPaymentcode result = getPaymentCodeFromPaymentCode(paymentCode);
            

            FrCurrency curr = result.getCurrencyId();
            
            Query q = em.createQuery("SELECT c FROM FrUsermoney  c where  c.userId.userId like '"+result.getFromUserId().getUserId()+"' and c.currencyId.currencyId = '"+curr.getCurrencyId()+"'");
            List<FrUsermoney> rs = (List<FrUsermoney>) q.getResultList();
            FrUsermoney user1= rs.get(0);
            
            Query q1 = em.createQuery("SELECT c FROM FrUsermoney  c where  c.userId.userId like '"+result.getToUserId().getUserId()+"' and c.currencyId.currencyId = '"+curr.getCurrencyId()+"'");
            List<FrUsermoney> rs2 = (List<FrUsermoney>) q1.getResultList();
            FrUsermoney user2= rs2.get(0);
             
            float vl=user1.getVal()-result.getVal();
            
            Query q3 = em.createQuery("UPDATE  FrUsermoney c SET   c.val = '"+vl+"' where c.currencyId.currencyId = '"+result.getCurrencyId().getCurrencyId()+"' and c.userId.userId like '"+user1.getUserId().getUserId()+"'");
            q3.executeUpdate();
            
            
            float vl2=user1.getVal()+result.getVal();
            Query q4 = em.createQuery("UPDATE  FrUsermoney c SET   c.val = '"+vl2+"' where c.currencyId.currencyId = '"+result.getCurrencyId().getCurrencyId()+"' and c.userId.userId like '"+user2.getUserId().getUserId()+"'");
            q4.executeUpdate();
            
            
            em.createQuery("UPDATE  FrPaymentcode c SET c.status = '0' where c.codeId = '"+result.getCodeId()+"'").executeUpdate();
            
                   
            
            return 1;
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return 0;
        }
        
    }

    @Override
    public List<FrPaymentcode> getAllPaymentCode() {
        try {
            

            Query q = em.createQuery("select s from FrPaymentcode s ");
            
            List<FrPaymentcode> result = (List<FrPaymentcode>)q.getResultList();
            
            return result;
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public FrPaymentcode getPaymentCodeFromPaymentCode(String paymentCode) {
        try {
            
            
            Query q = em.createQuery("select s from FrPaymentcode s where s.status=1 and s.paymentCode like '"+paymentCode+"'");
            
            FrPaymentcode result = (FrPaymentcode)q.getResultList().get(0);
            
            return result;
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    
    
}
